<?php
/**
 * Created by Sublime.
 * 退款处理类
 * @Author lingyun 
 * @Time:  2017-11-23 19:23:03
 */
namespace app\store\service;
use think\Db;
use think\Model;
use think\Session;
use app\comm\model\User;
use app\comm\model\OrderDetails;
use app\comm\model\Order;
use app\comm\model\UserWalletlog;
use app\comm\model\UserPointlog;
use app\comm\model\RefundLog;
use app\comm\model\OrderAction;
use app\comm\model\OrderBack;
use app\comm\model\StoreGoodsStock;
use app\comm\model\StoreGoodsLog;
class RefundService{	
	/**
	 * 余额退款
	 */
	public function refund_credit($order,$post){
		$orderdetail_model = new OrderDetails();	
		$user_model = new User();	
		$user_wallet_model = new UserWalletlog();
		$refund_log_model = new RefundLog();
		$order_action_model = new OrderAction();
		$order_back_model = new OrderBack();
		$order_model = new Order();
		$storegoodsstock_model = new StoreGoodsStock();
		$storegoodslog_model = new StoreGoodsLog();
		$admin_id = session('user')['id'];			//当前操作管理员id
		if($order['pay_status'] == 0){
			return ['code'=>0,'msg'=>'该订单未支付，不能发起退款申请！'];
		}
		Db::startTrans();		//开启事务
		try{
			$res = 1;
			//更新订单处理记录
			$res = $orderdetail_model->save(['id'=>$post['id'],'th_deal_admin'=>$admin_id,'th_deal_time'=>time(),'th_deal_result'=>2,'th_deal_type'=>$post['th_deal_type'],'th_deal_remark'=>$post['th_deal_remark']],['id'=>$post['id']]);
			//记录退款详情记录
			($res!==false) && $res = $refund_log_model->save(['store_id'=>$order['store_id'],'order_id'=>$order['order_id'],'admin_id'=>$admin_id,'amount'=>$order['tuihuo_amount'],'type'=>3,'addtime'=>time()]);
	 		//向会员退款，增加会员的余额值
			$user = $user_model->where('uid',$order['uid'])->find();
			$cur_amount = $user['user_money']+$order['tuihuo_amount'];		//会员当前余额
			($res!==false) && $res = $user_model->where('uid',$order['uid'])->setInc('user_money',$order['tuihuo_amount']);
			//插入会员余额变更记录
			($res!==false) && $res = $user_wallet_model->save(['uid'=>$order['uid'],'admin_id'=>$admin_id,'store_id'=>$order['store_id'],'order_id'=>$order['order_id'],'order_sn'=>$order['order_sn'],'uname'=>$user['realname'],'amount'=>$order['tuihuo_amount'],'cur_amount'=>$cur_amount,'sign'=>1,'chargetype'=>2,'charge_cn'=>'商品退款返还余额','addtime'=>time()]);
			//退款通过，扣除对应的积分
			$del_score = $order['give_score']*$order['tuihuo_num'];		//减去对应的积分
			($res!==false) && $res = $order_model->where('order_sn',$order['order_sn'])->setDec('score',$del_score);//更新将来发放的积分
			//更新订单动作记录
			($res!==false) && $res = $order_action_model->save(['uid'=>$order['uid'],'order_id'=>$order['order_id'],'goods_id'=>$order['goods_id'],'action_user'=>$admin_id,'order_status'=>6,'shipping_status'=>0,'pay_status'=>1,'status_desc'=>'退款申请已处理','storeid'=>$order['store_id'],'addtime'=>time()]);
			// ($res!==false) && $res = $order_back_model->save(['admin_id'=>$admin_id,'order_detail_id'=>$order['id'],'type'=>$order['tuihuo_sign'],'num'=>$order['tuihuo_num'],'amount'=>$order['tuihuo_amount'],'goodssid'=>$order['goods_id'],'addtime'=>time()]);
			//可二次销售的商品，入库  type=1
			if(($res !== false) && ($post['th_deal_type'] == 1) && ($order['spec_id']>0)){		//有商品属性
				$res = $storegoodsstock_model->where(['goods_spec_id'=>$order['spec_id'],'goods_id'=>$order['goods_id']])->setInc('stock',$order['tuihuo_num']);
				$cur_stock_num = $storegoodsstock_model->where(['goods_id'=>$order['goods_id'],'goods_spec_id'=>$order['spec_id']])->value('stock');			//当前商品属性的库存
			}elseif(($res !== false) && ($post['th_deal_type'] == 1) && ($order['spec_id']<=0)){	//无商品属性
				$res = $storegoodsstock_model->where('goods_id',$order['goods_id'])->setInc('stock',$order['tuihuo_num']);
				$cur_stock_num = $storegoodsstock_model->where('goods_id',$order['goods_id'])->value('stock');			//当前商品属性的库存
			}	
			//生成入库记录
			// $ruku_num = $cur_stock_num+$order['tuihuo_num'];
			($res!==false) && ($post['th_deal_type']== 1) && ($order['spec_id'] > 0) && $res = $storegoodslog_model->save(['goods_id'=>$order['goods_id'],'goods_spec_id'=>$order['spec_id'],'title'=>$order['spec_detail'],'order_id'=>$order['order_id'],'store_id'=>$order['store_id'],'tuihuo_num'=>$order['tuihuo_num'],'remark'=>'订单退货入库','cur_stock'=>$cur_stock_num,'addtime'=>time()]);
			($res!==false) && ($post['th_deal_type']== 1) && ($order['spec_id'] <= 0) && $res = $storegoodslog_model->save(['goods_id'=>$order['goods_id'],'goods_spec_id'=>'-1','title'=>$order['spec_detail'],'order_id'=>$order['order_id'],'store_id'=>$order['store_id'],'tuihuo_num'=>$order['tuihuo_num'],'remark'=>'订单退货入库','cur_stock'=>$cur_stock_num,'addtime'=>time()]);
			if($res!==false){
				// 提交事务
    			Db::commit(); 
    			return ['code'=>1,'退货成功'];
			}else{
				return ['code'=>0,'退货失败'];
			}
		}catch(\Exception $e) {
		    // 回滚事务
		    Db::rollback();
		    return ['code'=>0,'退货失败'];
		}
	}

}

?>